<div id="dialog-editPreferLink" class="form-horizontal">
	<div class="modal-body">
	   <div class="form-group">
	       <label class="col-sm-3 control-label">网站地址</label>
	       <div class="col-sm-9">
 	           <input class="form-control" type="text" name="url" placeholder="请填写网站地址" style="width: 100%"/>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	   <div class="form-group">
	   	   <label class="col-sm-3 control-label">类型</label>
		   <div class="col-sm-9">
			   <select class="form-control" name="type" style="width: 100%">   
			   		<option value="1">网站</option> 
			        <option value="2">链接</option>
			        <option value="3">服务器</option>   
			   </select>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	   <div class="form-group">
	   	   <label class="col-sm-3 control-label">描述</label>
	       <div class="col-sm-9">
 	           <textarea class="form-control" type="text" name="content" style="width: 100%"/>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	</div>
    <div class="modal-footer">
	   <div class="form-group">
	       <label class="col-sm-3 control-label">网站名称</label>
	       <div class="col-sm-9">
 	           <input class="form-control" type="text" name="name" placeholder="自定义网站名称" style="width: 100%"/>
	       </div>
	       <div class="clearfix"></div>
	   </div>
	</div>
    <div class="modal-footer">
	    <div class="form-group">
		    <div class="col-sm-12 text-right">
		        <button  type="button" class="btn btnPrimary" onclick="doEditPreferLink();" style="width: 100px;height: 42px;">确定</button>
		        <button  type="button" class="btn btnPrimary" onclick="cancelEditPreferLink();" style="width: 100px;height: 42px;">取消</button>
			</div>
		</div>
	</div>
</div>

<script src="js/md5.js"></script>
<script type="text/javascript">
	function editPreferLinkPageInit(_preferLinkInfo, _callback)
	{
		console.log("editPreferLinkPageInit(): _preferLinkInfo", _preferLinkInfo);
		
		EditPreferLink.init(_preferLinkInfo, _callback);
	}
	
	function closeEditPreferLinkDialog()
	{
		closeBootstrapDialog("editPreferLink");
		//临时方案避免滚动条消失
		window.location.reload();
	}

	function doEditPreferLink()
	{
		EditPreferLink.doEditPreferLink();
		//临时方案避免滚动条消失
		window.location.reload();
	}
	
	function cancelEditPreferLink()
	{
		closeEditPreferLinkDialog();
	}
	
	var EditPreferLink = (function () {
		var callback;
		var preferLinkInfo;

		function init(_preferLinkInfo, _callback)
		{
			console.log("EditPreferLink.init() _preferLinkInfo:", _preferLinkInfo);
			if(_preferLinkInfo)
			{
				preferLinkInfo = _preferLinkInfo;
				var url = preferLinkInfo.url;
				var name = preferLinkInfo.name;
				var content = preferLinkInfo.content;
				var type = preferLinkInfo.type;
				if(url)
				{
					$("#dialog-editPreferLink input[name='url']").val(url);
				}
				if(name)
				{
					$("#dialog-editPreferLink input[name='name']").val(name);
				}
				if(content)
				{
					$("#dialog-editPreferLink textarea[name='content']").val(content);
				}
				if(type)
				{
					$("#dialog-editPreferLink select[name='type']").val(type);
				}
			}			
			
			callback = _callback;
						
		}
		
		function doEditPreferLink()
		{
			var isChanged = false;
			var url;
			var name;
			var content;
			var type;
			var tmpUrl = $("#dialog-editPreferLink input[name='url']").val();
			if(!(preferLinkInfo.serverUrl && tmpServerUrl === preferLinkInfo.serverUrl))
			{				
				isChanged = true;
				url = tmpUrl;
				console.log("doEditPreferLink url changed:", url);
			}
				
			var tmpName = $("#dialog-editPreferLink input[name='name']").val();
			if(!(preferLinkInfo.name && tmpName === preferLinkInfo.name))
			{
				isChanged = true;
				name = tmpName;
				console.log("doEditPreferLink name changed:", name);
			}
			
			var tmpContent = $("#dialog-editPreferLink textarea[name='content']").val();
			if(!(preferLinkInfo.content && tmpContent === preferLinkInfo.content))
			{
				isChanged = true;
				content = tmpContent;
				console.log("doEditPreferLink content changed:", content);
			}
			
			var tmpType = $("#dialog-editPreferLink select[name='type']").val();
		    if(!(preferLinkInfo.type && tmpType === preferLinkInfo.type))
		    {
		    	isChanged = true;
		    	type = tmpType;
				console.log("doEditPreferLink type changed:", type);
		    }	

		    if(isChanged == true)
			{
		  	  	editPreferLink(name, url, content, type);
		      	return true;
			}
			else
			{
		    	alert("设置未改动！");
		        return false;
		    }
		}
		
	   	function editPreferLink(name, url, content, type)
	   	{
	    	$.ajax({
	             url : "/DocSystem/Bussiness/editPreferLink.do",
	             type : "post",
	             dataType : "json",
	             data : {
	            	linkId: preferLinkInfo.id,
	            	name: name,
	            	url: url,
	            	content: content,
	            	type: type,
	             },
	             success : function (ret) {
	            	console.log("editPreferLink ret:", ret);            		
	             	if( "ok" == ret.status){             		
	             		// 普通消息提示条
						bootstrapQ.msg({
									msg : "修改成功！",
									type : 'success',
									time : 2000,
						});
	             		
	             		callback && callback();
	                }
	                else
	                {
	                	showErrorMessage("修改失败:" + ret.msgInfo);
	                }
	            },
	            error : function () {
	                showErrorMessage("修改失败:服务器异常！");
	            }
	        });
	    }
	   	
		//开放给外部的调用接口
        return {
			init: function(_preferLinkInfo, _callback){
				init(_preferLinkInfo, _callback);
			},
			doEditPreferLink: function(){
				doEditPreferLink();
            },
        };
	})();
</script>

